package com.wontube.video.controller;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.extension.utils.net.URLUtils;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.IShortFilmHandler;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.video.pub.AppConfig;
import com.wontube.video.pub.VideoCookieUtils;
import com.wontube.web.i18n.I18nMethod;

/**
 * 首页控制器
 * 
 * @author LuoBiao(2012-4-5)
 */
@Controller
public class HomeController {
	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping("")
	public ModelAndView videoHome(HttpServletRequest request, HttpServletResponse response) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (localeName == null) {
			localeName = "en_US";
			VideoCookieUtils.setLocaleCookie(response, localeName);
		}		

		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));

		mv.addObject("locale", localeName);
		String youtubeUrl = request.getParameter("url");
		if (youtubeUrl != null && !"".equals(youtubeUrl)) {
			Map<String, String> map = URLUtils.parseQueryString(youtubeUrl);
			String v = map.get("v");
			if (v != null) {
				try {
					response.sendRedirect("download/" + v);
				} catch (IOException e) {
				}
				return null;
			}
		}
		mv.setViewName("index");
		return mv;
	}

	/**
	 * 美国首页 en_US
	 * 
	 * @return
	 */
	@RequestMapping("/eng")
	public ModelAndView engHome(HttpServletResponse response) {
		return this.gotoHome(response, "en_US");
	}

	/**
	 * 日本首页 ja_JP
	 * 
	 * @return
	 */
	@RequestMapping("/jp")
	public ModelAndView jpHome(HttpServletResponse response) {
		return this.gotoHome(response, "ja_JP");
	}

	/**
	 * 德国首页 de_DE
	 * 
	 * @return
	 */
	@RequestMapping("/de")
	public ModelAndView deHome(HttpServletResponse response) {
		return this.gotoHome(response, "de_DE");
	}

	/**
	 * 法国首页 fr_FR
	 * 
	 * @return
	 */
	@RequestMapping("/fr")
	public ModelAndView frHome(HttpServletResponse response) {
		return this.gotoHome(response, "fr_FR");
	}

	/**
	 * 中国台湾首页 zh_TW
	 * 
	 * @return
	 */
	@RequestMapping("/zh_tw")
	public ModelAndView zhtwHome(HttpServletResponse response) {
		return this.gotoHome(response, "zh_TW");
	}

	/**
	 * 意大利首页 it_IT
	 * 
	 * @return
	 */
	@RequestMapping("/it")
	public ModelAndView itHome(HttpServletResponse response) {
		return this.gotoHome(response, "it_IT");
	}

	/**
	 * 西班牙首页 en_CA
	 * 
	 * @return
	 */
	@RequestMapping("/es")
	public ModelAndView esHome(HttpServletResponse response) {
		return this.gotoHome(response, "en_CA");
	}

	/**
	 * 匈牙利首页 ko_KR
	 * 
	 * @return
	 */
	@RequestMapping("/hu")
	public ModelAndView huHome(HttpServletResponse response) {
		return this.gotoHome(response, "ko_KR");
	}

	/**
	 * 阿拉伯语pa_PK
	 * @param request
	 * @return
	 */
	@RequestMapping("/pa")
	public ModelAndView paHome(HttpServletResponse response){
		return this.gotoHome(response, "pa_PK");
	}
	
	@RequestMapping("/top-n-funniest-youtube-video")
	public ModelAndView toTop10(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/top-n-funniest-youtube-video");
		return mv;
	}

	@RequestMapping("link")
	public ModelAndView toLink(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/link");
		return mv;
	}

	@RequestMapping(value = "/download/{videoId}", method = RequestMethod.GET)
	public ModelAndView download(@PathVariable("videoId") String videoId, 
			HttpServletRequest request, HttpServletResponse response) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = this.getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("index");
		
		String userAgent = request.getHeader("User-Agent");
		if (userAgent == null) {
			userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5";
		}
		
		mv.addObject("userAgent", userAgent);

		Remoting<IShortFilmHandler> remoting = new Remoting<IShortFilmHandler>();
		IShortFilmHandler handler = remoting.createService(IShortFilmHandler.class);
		Movie film = handler.getShortFilm("yt" + videoId);
		
		if (film == null) {
			try {
				response.sendError(HttpURLConnection.HTTP_NOT_FOUND);
			} catch (IOException e) {
			}
			return null;
		}

		mv.addObject("film", film);

		return mv;
	}

	@RequestMapping(value = "/addon", method = RequestMethod.GET)
	public ModelAndView toAddon(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/addon");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-downloader-chrome", method = RequestMethod.GET)
	public ModelAndView toAddonChrome(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/youtube-downloader-chrome");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-downloader-firefox", method = RequestMethod.GET)
	public ModelAndView toAddonFirefox(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/youtube-downloader-firefox");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-downloader-ie", method = RequestMethod.GET)
	public ModelAndView toAddonIE(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/youtube-downloader-ie");
		return mv;
	}
	
	@RequestMapping(value = "/youtube-downloader-safari", method = RequestMethod.GET)
	public ModelAndView toAddonSafari(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/youtube-downloader-safari");
		return mv;
	}

	@RequestMapping(value = "/about", method = RequestMethod.GET)
	public ModelAndView toAbout(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/about");
		return mv;
	}

	@RequestMapping(value = "/faq")
	public ModelAndView toFaq(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/faq");
		return mv;
	}
	
	@RequestMapping(value = "/sitemap")
	public ModelAndView toSitemap(HttpServletRequest request) {
		String localeName = VideoCookieUtils.getLocaleName(request);
		if (StringUtils.isNullOrEmpty(localeName)) {
			localeName = "en_US";
		}
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localeName));
		mv.addObject("locale", localeName);
		mv.setViewName("pub/sitemap");
		return mv;
	}

	private ModelAndView getI18nView(Locale locale) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("i18n", new I18nMethod(AppConfig.widgetBundleName, locale));
		return mv;
	}

	private ModelAndView gotoHome(HttpServletResponse response, String localName) {
		//RequestHeadersHolder.holder.putProperty("localeName", localName);
		VideoCookieUtils.setLocaleCookie(response, localName);
		
		ModelAndView mv = getI18nView(AppConfig.getLocale(localName));
		mv.addObject("locale", localName);
		mv.setViewName("index");

		return mv;
	}
}
